/**
 * 
 */
package org.springside.examples.miniweb.web.report;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.examples.miniweb.entity.report.ColdPumpReport;
import org.springside.examples.miniweb.entity.report.CompressorReport;
import org.springside.examples.miniweb.entity.search.SearchForm;
import org.springside.examples.miniweb.service.account.AccountManager;
import org.springside.examples.miniweb.service.customer.CustomerManager;
import org.springside.examples.miniweb.service.report.ColdPumpReportManager;
import org.springside.examples.miniweb.web.IndexPageController;

/**
 * @author Eric 2012-3-6
 * 
 * @since 6.0
 * 
 */
@Controller
@RequestMapping(value = "/report/cold")
public class ColdPumpReportController extends ReportController<ColdPumpReport>{

	private ColdPumpReportManager<ColdPumpReport> manager;

	private AccountManager accountManager;
	
	private CustomerManager customerManager;
	
	/**
	 * @param customerManager the customerManager to set
	 */
	@Autowired
	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}
	
	/**
	 * @param manager the manager to set
	 */
	@Autowired
	public void setManager(ColdPumpReportManager<ColdPumpReport> manager) {
		this.manager = manager;
	}
	
	@RequestMapping(value = { "list" })
	public String list(Model model) {
		Page<CompressorReport> pages = new PageImpl<CompressorReport>(new ArrayList<CompressorReport>());
		initListPage(model);
		model.addAttribute("search", new SearchForm());
		model.addAttribute("reports", pages.getContent());
		model.addAttribute("pages", pages);
		return "reports/coldPumpList";
	}
	
	@RequestMapping(value = "create")
	public String createForm(Model model, HttpServletRequest request) {
		model.addAttribute("add", true);
		model.addAttribute("customers", customerManager.getAllCustomer());
		model.addAttribute("users", accountManager.getAllUser());
		model.addAttribute("report", new ColdPumpReport());
		return "reports/coldPumpForm";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "search")
	public String search(@ModelAttribute("search") SearchForm searchFrom, Model model, HttpServletRequest request) {
		model.addAttribute("search", searchFrom);
		initListPage(model);
		List<ColdPumpReport> list = null;
		if (searchFrom.getPageNumber().equalsIgnoreCase("research")) {
			list = manager.getReportByUserName(searchFrom.getUserName());
			list = Collections.synchronizedList(list);
			if (searchFrom.getCustomerName() != null
					&& !searchFrom.getCustomerName().equalsIgnoreCase(
							"allCustomers")) {
				for (int i = 0; i < list.size(); i++) {
					ColdPumpReport report = list.get(i);
					if (!report.getCustomerName().equalsIgnoreCase(
							searchFrom.getCustomerName())) {
						list.remove(report);
						i--;
					}
				}
			}

			if (searchFrom.getStartDate() != null) {
				for (int i = 0; i < list.size(); i++) {
					ColdPumpReport report = list.get(i);
					if (report.getUpdateTime()
							.before(searchFrom.getStartDate())) {
						list.remove(report);
						i--;
					}
				}
			}

			if (searchFrom.getEndDate() != null) {
				for (int i = 0; i < list.size(); i++) {
					ColdPumpReport report = list.get(i);
					if (report.getUpdateTime().after(searchFrom.getEndDate())) {
						list.remove(report);
						i--;
					}
				}
			}
			searchFrom.setPageNumber("0");
			request.getSession().setAttribute("coldReportList", list);
		} else {
			list = (List<ColdPumpReport>) request.getSession().getAttribute(
					"coldReportList");
		}
		
		Page<ColdPumpReport> pages = getPages(searchFrom, list);
		model.addAttribute("reports", pages.getContent());
		model.addAttribute("pages", pages);
		return "reports/coldPumpList";
	}

	/**
	 * @param model
	 */
	private void initListPage(Model model) {
		model.addAttribute("customers", customerManager.getAllCustomer());
		model.addAttribute("users", accountManager.getAllUser());
	}
	
	
	@RequestMapping(value = "save/*")
	public String save(ColdPumpReport report, RedirectAttributes redirectAttributes) {
		manager.saveReport(report);
		redirectAttributes.addFlashAttribute("message", "冷泵维修记录" + report.getReportId() + "更新成功");
		if(IndexPageController.isAdminRole() || IndexPageController.isSuperRole()){
			return "redirect:/report/cold/list";
		}
		return "redirect:/process/";
		
	}
	
	
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		manager.deleteReport(id);
		redirectAttributes.addFlashAttribute("message", "删除冷泵维修记录成功");
		return "redirect:/report/cold/list";
	}
	
	@RequestMapping(value = "update/{id}")
	public String update(@PathVariable("id") Long id, Model model, RedirectAttributes redirectAttributes) {
		initListPage(model);
		model.addAttribute("report", manager.getOneById(id));
//		redirectAttributes.addFlashAttribute("message", "维修压缩机维修记录" + id + "成功");
		return "reports/coldPumpForm";
	}
	
	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	
	@RequestMapping(value = "exportExcel")
	public String exportExcel(Model model, HttpServletRequest request ) {
		@SuppressWarnings("unchecked")
		List<CompressorReport> list = (List<CompressorReport>) request.getSession().getAttribute(
				"coldReportList");
		model.addAttribute("coldReports", list);
		return "coldExcel";
	}
	
	@RequestMapping(value = "exportAllExcel")
	public String exportAllExcel(Model model, HttpServletRequest request ) {
		List<ColdPumpReport> list = manager.getAllReports();
		model.addAttribute("coldReports", list);
		return "coldExcel";
	}
}
